package com.eirikb.server;

import com.eirikb.shared.events.ErrorEvent;
import com.eirikb.shared.events.Event;
import com.eirikb.shared.events.JoinEvent;
import com.eirikb.shared.events.KickEvent;
import com.eirikb.shared.events.LoginEvent;
import com.eirikb.shared.events.MessageEvent;
import com.eirikb.shared.events.NickChangeEvent;
import com.eirikb.shared.events.NickTakenEvent;
import com.eirikb.shared.events.PartEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.schwering.irc.lib.IRCConnection;
import org.schwering.irc.lib.IRCEventListener;
import org.schwering.irc.lib.IRCModeParser;
import org.schwering.irc.lib.IRCUser;

/**
 *
 * @author eirikb
 */
public class ServerUser implements IRCEventListener {

    private String nick;
    private ArrayList<Event> events;
    private IRCConnection irc;
    private boolean ping;

    public ServerUser(Server server, String nick, String name) {
        this.nick = nick;
        events = new ArrayList<Event>();
                irc = new IRCConnection("irc.homelien.no",
                6667,
                6669,
                null,
                nick,
                nick,
                name);
        irc.addIRCEventListener(this);
        irc.setDaemon(true);
        irc.setColors(false);
        irc.setPong(true);
    }

        public Boolean connect() {
        try {
            irc.connect();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(ServerUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public Event[] getEvents() {
        Event[] all = events.toArray(new Event[events.size()]);
        events.clear();
        return all;
    }

    public void ping() {
        ping = true;
    }

    public boolean getPing() {
        boolean pong = ping;
        ping = false;
        return pong;
    }

    public void disconnect() {
        irc.doQuit("Tihldelan ftw!");
    }
    
     public void changeNick(String nick) {
        irc.doNick(nick);
    }

    public void sendMessage(String msg) {
        irc.doPrivmsg("#tihldelan", msg);
    }

    public void onDisconnected() {
        System.out.println("Disconnected.");
    }

    public void onError(String msg) {
        System.out.println("ERROR: " + msg);
    }

    public void onError(int num, String msg) {
        System.out.println("Error! " + num + " - " + msg);
        switch (num) {
            case 433:
                events.add(new NickTakenEvent());
                break;
            default:
                events.add(new ErrorEvent(num, msg));
                System.out.println("Ukjent feil!");
                break;
        }
    }

    public void onInvite(String chan, IRCUser user, String nickPass) {
        System.out.println("INVITE: " + user.getNick() + " invites " + nickPass + " to " + chan);
    }

    public void onJoin(String chan, IRCUser user) {
        System.out.println("JOIN: " + user.getNick() + " joins " + chan);
        if (!user.getNick().equals(irc.getNick())) {
            events.add(new JoinEvent(user.getNick(), user.getUsername()));
        }
    }

    public void onKick(String chan, IRCUser user, String nickPass, String msg) {
        System.out.println("KICK: " + user.getNick() + " kicks " + nickPass + "(" + msg + ")");
        events.add(new KickEvent(user.getNick(), nickPass, msg));
    }

    public void onMode(String chan, IRCUser user, IRCModeParser modeParser) {
        System.out.println("MODE: " + user.getNick() + " changes modes in " + chan + ": " + modeParser.getLine());
        events.add(new MessageEvent("* ", "MODE (" + user.getNick() + "): " + modeParser.getLine()));
    }

    public void onNick(IRCUser user, String nickNew) {
        System.out.println("NICK: " + user.getNick() + " is now known as " + nickNew);
        System.out.println("OMGOMGOGOGMGOMGOMGOGMOGMGOMGOMGOGMOGMGOMG");
        events.add(new NickChangeEvent(user.getNick(), nickNew));
    }

    public void onPart(String chan, IRCUser user, String msg) {
        System.out.println("PART: " + user.getNick() + " parts from " + chan + "(" + msg + ")");
        if (!user.getNick().equals(irc.getNick())) {
            events.add(new PartEvent(user.getNick(), msg));
        }
    }

    public void onPrivmsg(String target, IRCUser user, String msg) {
        System.out.println("PRIVMSG: " + user.getNick() + " to " + target + ": " + msg);
        events.add(new MessageEvent(user.getNick(), msg));
    }

    public void onQuit(IRCUser user, String msg) {
        System.out.println("QUIT: " + user.getNick() + " (" +
                user.getUsername() + "@" + user.getHost() + ") (" + msg + ")");
        if (!user.getNick().equals(irc.getNick())) {
            events.add(new PartEvent(user.getNick(), msg));
        }
    }

    public void onTopic(String chan, IRCUser user, String topic) {
        System.out.println("TOPIC: " + user.getNick() + " changes topic of " + chan + " into: " + topic);
    }

    public void onRegistered() {
        System.out.println("Registered!");
        irc.doJoin("#tihldelan");
    }

    public void onReply(int num, String value, String msg) {
        System.out.println("REPLY: " + num + " " + value + " - " + msg);
        switch (num) {
            case 353:
                StringTokenizer st = new StringTokenizer(msg, " ");
                System.out.println("Nicks:");
                while (st.hasMoreTokens()) {
                    events.add(new JoinEvent(st.nextToken(), ""));
                }
                break;
            case 366:
                System.out.println("LOGINEVENT JAY!");
                events.add(new LoginEvent());
                break;
        }
    }

    public void onMode(IRCUser user, String passiveNick, String mode) {
        System.out.println("MODE: " + user + " " + passiveNick + " " + mode);
    }

    public void onNotice(String target, IRCUser user, String msg) {
        System.out.println("NOTICE: " + target + " " + user + " " + msg);
    }

    public void onPing(String msg) {
        System.out.println("PING: " + msg);
    }

    public void unknown(String arg0, String arg1, String arg2, String arg3) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
